package eg.music
{
import mx.formatters.DateFormatter;
import com.fit4as.fit.Exception;

public class MusicDate
{
    public var dateFormatString : String = "M/D/YY K:NN A";

    private var date : Date;

    public function MusicDate(date : Date)
    {
        this.date = date;
    }

    public static function parse(s : String) : MusicDate
    {
        var pattern : RegExp = /(\d+)\/(\d+)\/(\d+) (\d+):(\d+) (am|pm)/i;
        var result : Array = pattern.exec(s) as Array;
        if (result == null) throw new Exception("DateFormatException: For input string: \"" + s + "\"");

        var month : int = parseInt(result[1], 10);
        var day : int = parseInt(result[2], 10);
        var year : int = parseInt(result[3], 10);
        var hours : int = parseInt(result[4], 10);
        var minutes : int = parseInt(result[5], 10);
        var amPm : String = result[6];
        if (amPm.toLowerCase() == "pm") hours += 12;
        var date : Date = new Date(year, month - 1, day, hours, minutes, 0);
        return new MusicDate(date);
    }

    public function toString() : String
    {
        var formatter : DateFormatter = new DateFormatter();
        formatter.formatString = dateFormatString;
        return formatter.format(this.date);
    }

    public function equals(date : MusicDate) : Boolean
    {
        return this.toString() == date.toString();
    }
}
}